<?php

namespace app\components\pagination;

use yii\base\Component;

class Pagination extends Component
{
    public $pageParam = "page"; //分页参数
    public $pageSize = 10; //每页条数
    public $totalCount = 0; //总条数
    public $totalPage = 0;
    public $page; //当前页

    public function __construct(array $config = [])
    {
        parent::__construct($config);
    }

    public function init()
    {
        if(empty($this->page)){
            $this->page = \Yii::$app->getRequest()->get($this->pageParam);
        }

        $page = $this->totalCount / $this->pageSize;
        $this->totalPage = ($this->totalCount % $this->pageSize) == 0 ? $page : (int)$page + 1;

        if($this->page < 0 || $this->totalPage == 0){
            $this->page = 1;
        }
//        if($this->page > $this->totalPage){
//            $this->page = $this->totalPage;
//        }
    }

    public function getPage(){
        return $this->page;
    }

    public function getPageSize(){
        return $this->pageSize;
    }

    public function getLimit(){
        return $this->pageSize;
    }

    public function getOffset(){
        return ($this->getPage() - 1) * $this->getPageSize();
    }

    public function Out(){
        return [
            'total_page'=>$this->totalPage,
            'page'=>$this->page,
            'total_count'=>$this->totalCount,
        ];
    }
}